package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseTrademark;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/product")
public class BaseTrademarkController {

    @Autowired
    BaseTrademarkService baseTrademarkService;
//    品牌的分页功能实现
    @GetMapping("baseTrademark/{page}/{limit}")
    public Result baseTrademark(@PathVariable Integer page,
                                @PathVariable Integer limit){

        Page<BaseTrademark> trademarkPage = new Page<>(page, limit);
        Page<BaseTrademark> pn = baseTrademarkService.page(trademarkPage, null);
        return Result.ok(pn);
    }

//    品牌的查询
    @GetMapping("baseTrademark/get/{id}")
    public Result getBaseTrademarkById(@PathVariable("id") Long id){

        BaseTrademark trademark = baseTrademarkService.getById(id);
        return Result.ok(trademark);
    }


/* 发送的json
* {
  "tmName": "hhh",
  "logoUrl": "/static/default.jpg"
   }
   *
   * 正常情况下要生成vo类，但是前端提交的数据和数据库中的一致，就不用写vo类
*  */
//    品牌的保存
    @PostMapping("baseTrademark/save")
    public Result saveBaseTrademark(@RequestBody BaseTrademark trademark) {

        baseTrademarkService.save(trademark);
        return Result.ok();
    }

//    根据主键id来更改
    @PutMapping("baseTrademark/update")
    public Result updateBaseTrademark(@RequestBody BaseTrademark trademark) {

//        ？？？updateById后面为什么不加id
        baseTrademarkService.updateById(trademark);
        return Result.ok();
    }

//    删
    @DeleteMapping("remove/{removeId}")
    public Result removeBaseTrademark(@PathVariable Long removeId ) {

        baseTrademarkService.removeById(removeId);
        return Result.ok();
    }


//查品牌列表
//    http://api.gmall.com/admin/product/baseTrademark/getTrademarkList
    @GetMapping("baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> list = baseTrademarkService.list();
        return Result.ok(list);
    }
}
